// Copyright (C) 2021, Flickering Inc. All rights reserved.
// Author: haiquanchen (haiquanchen@flickering.ai)

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(cc.Color)
    lightColor: cc.Color = null;

    @property(cc.Vec4)
    lightPos: cc.Vec4 = null;

    @property(cc.Color)
    ambientLight: cc.Color = null;

    @property
    shininess: number = 10;

    @property({ type: [cc.Slider], displayName: '参数调节' })
    sliders: Array<cc.Slider> = [];

    private _material: cc.MaterialVariant;

    onLoad() {
        if (this.node.getComponent(cc.Label)) {
            this._material = this.node.getComponent(cc.Label).getMaterial(0);
        } else if (this.node.getComponent(cc.Sprite)) {
            this._material = this.node.getComponent(cc.Sprite).getMaterial(0);
        }
        this._material.setProperty('lightColor', this.lightColor);
        this._material.setProperty('lightPos', this.lightPos);
        this._material.setProperty('ambientLight', this.ambientLight);
        this._material.setProperty('shininess', this.shininess);
    }

    start() {}

    // update (dt) {}

    upX(e, data) {
        this._material.setProperty('lightX', this.sliders[0].progress * 4 - 2);
    }

    upY(e, data) {
        this._material.setProperty('lightY', this.sliders[1].progress * 4 - 2);
    }
    upZ(e, data) {
        this._material.setProperty('lightZ', this.sliders[2].progress * 4 - 1);
    }
    upLight(e, data) {
        this._material.setProperty('light', this.sliders[3].progress * 10 - 1);
    }
}
